/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
	
local DeathTime = 5;
	
--################# Count up deaths @Prefanatic
function PlayerDeathWW(victim, weapon, killer)

	-- Set up some variables used for the death think
	victim.deathtime = 0
	victim.nextsecond = CurTime() + 1
	victim:UnLock()
	
	if !killer:IsPlayer() then return false end -- If the killer isn't a player, then discontinue
	
	if killer == victim then return false end -- If the player killed himself, discontinue
	
	killer:SetFrags(killer:Frags() + 1) -- Set the frags
	
	if weapon:IsPlayer() and weapon == killer then -- If the weapon is a player, and the weapon is the killer, than fix the bug and make the weapon a weapon
		weapon = killer:GetActiveWeapon()
	end
	
	local weapon = weapon:GetClass() -- Get the class of the weapon
	local name = WW.Weapons[weapon] or weapon -- Try and format the weapon, if possible
	
	local TName = string.lower(killer:GetTeamName()) -- Get the team
	
	WW.Flavor.Scores[TName] = WW.Flavor.Scores[TName] + 1 -- Add the score
	UpdateScores() -- Update the scores
	
	local teamkilled = false
	
	if OnSameTeam( victim, killer ) then -- If they are on the same team then
		victim:EmitSound(table.Random(WW.Sounds.FF)) -- Emit a "we trusted you" sound when killed
		
		teamkilled = true
		
		WW.Flavor.Scores[TName] = WW.Flavor.Scores[TName] - 2 -- Subtract 2 points due to team killing
		
		if killer.TKKills == nil or killer.TKKills == 0 then -- If the killer doesn't have any team kills then
			killer.TKKills = 1 -- Set the team kills to 1
			timer.Simple(20, function() killer.TKKills = 0 end) -- Create a refresh timer to puge his kills
		else
			killer.TKKills = killer.TKKills + 1 -- add on kills
			
			if killer.TKKills > 5 then -- If the kills get higher than 5 then
				game.ConsoleCommand("kickid" .. killer:UserID() .. " Autokicked because of team killing\n") -- Kick him
				PrintMessageAll( killer:Nick() .. " was kicked due to team killing!") -- Notify everyone
				killer.TKKills = nil -- Remove the variable
			end
			
		end
		
	end
	
	victim:ShowKilledView( killer, teamkilled ) -- Send the message to show the killed view
	teamWin(killer) -- Check if the team has won
	
end
hook.Add("PlayerDeath", "PlayerDeathWW", PlayerDeathWW)

--################# Constantly checked if they are winning @Prefanatic
function teamWin( ply )

	local TName = string.lower(ply:GetTeamName())
	
	if WW.Flavor.Scores[TName] >= 7 then
		for _,v in pairs(player.GetAll()) do
			v:Lock()
			v:PrintMessage( HUD_PRINTTALK, TName .. " has won the game with " .. WW.Flavor.Scores[TName] .. " points!" )
			v:PrintMessage( HUD_PRINTTALK, "Please wait, the game will continue in a few seconds.  We need to clean up the map!" )
			timer.Simple(5, function() v:Unlock() ResetScores() v:SetFrags(0) v:KillSilent() UpdateScores() game.CleanUpMap() end)
		end
	end
	
end

function UpdateScores()

	for k, v in pairs(player.GetAll()) do
	
		local oldv = v
		for k,v in pairs(WW.Flavor.Scores) do
			oldv:SetNWInt(WW.Flavor.Scores[k].."score", v)
		end
		
	end
	
end

function PlayerRespawnThink(ply)
	if CurTime() > ply.nextsecond then
		if ply.deathtime < DeathTime then
			ply.deathtime = ply.deathtime + 1
			ply.nextsecond = CurTime() + 1
			if DeathTime - ply.deathtime == 0 then
				Notify(ply, "Respawning Now!", 2)
			else
				Notify(ply, "Respawn in: " .. DeathTime - ply.deathtime, 1, true)
			end
			
			if DeathTime - ply.deathtime == 4 then
				ply:SendSound(WW.Sounds.End2)
			end
		else
			ply.deathtime = nil
			ply.nextsecond = nil 
		--	ply.Spawned = true
			ply:Spawn()
		end
	end
end
hook.Add("PlayerDeathThink", "RespawnThinkWW", PlayerRespawnThink)

function AutoBallence()
end